getApp();
var t = 0;
module.exports = Behavior({
  properties: {
    P_rpx: { type: Number, value: 1 },
    P_dpr: { type: Number, value: 1 },
  },
  data: { P_imgs: {}, share_data: {} },
  methods: {
    getImgs: function (t, e, a) {
      var r = function (r) {
        if (Object.prototype.hasOwnProperty.call(e, r)) {
          var i = e[r],
            o = t.createImage();
          (o.onload = function () {
            a.data.P_imgs[r] = o;
          }),
            (o.src = i || '/images/potu.png'),
            (o.onerror = function (t) {
              (o.src = '/images/potu.png'),
                console.error(
                  '获取图片时出现错误。若多次出现此提示，请截图反馈给开发，key为'
                    .concat(r, '; 其值为')
                    .concat(i, '; \nerr: ')
                    .concat(t),
                ),
                wx.hideLoading(),
                wx.showModal({
                  title: '获取图片资源异常',
                  content: t,
                  showCancel: !1,
                });
            });
        }
      };
      for (var i in e) r(i);
    },
    drawFillRect: function (t, e, a, r, i, o, n, s) {
      (t.lineWidth = o),
        (t.fillStyle = s),
        (t.strokeStyle = n),
        t.fillRect(e, a, r, i),
        t.strokeRect(e, a, r, i);
    },
    drawFillRect_radius: function (t, e, a, r, i, o, n, s) {
      t.save(),
        t.beginPath(),
        (t.fillStyle = n),
        t.moveTo(e + o, a),
        t.arcTo(e + r, a, e + r, a + i, o),
        t.arcTo(e + r, a + i, e, a + i, o),
        t.arcTo(e, a + i, e, a, o),
        t.arcTo(e, a, e + r, a, o),
        t.closePath(),
        (t.strokeStyle = n),
        t.stroke(),
        t.clip(),
        s && t.fillRect(e, a, r, i);
    },
    updataToast: function (t) {
      for (var e = 0, a = 0; a < t.length; a++) {
        t[a] && e++;
      }
      var r = parseFloat(String((e / (t.length + 1)) * 100)).toFixed(2) + '%';
      wx.showLoading({ title: '绘制中' + r });
    },
    getShare: function (e) {
      var a = this;
      this.data.share_data = e;
      var r = this;
      return new Promise(function (i, o) {
        wx.createSelectorQuery()
          .in(a)
          .select('#Canvas_SQ')
          .fields({ node: !0, size: !0 })
          .exec(function (o) {
            var n = a.data.P_dpr,
              s = {
                head_img: e.chatroom_head_img,
                qr: e.qrcode_url,
                wx_logo: '/images/else/wx_logo_black.png',
              },
              c = o[0].width,
              h = o[0].height,
              d = o[0].node,
              l = d.getContext('2d');
            (d.width = c * n), (d.height = h * n);
            d.requestAnimationFrame(function e() {
              clearTimeout(t),
                (t = a.render(d, l)
                  ? setTimeout(function () {
                      wx.canvasToTempFilePath(
                        {
                          canvas: d,
                          quality: 1,
                          success: function (t) {
                            i(t.tempFilePath), wx.hideLoading();
                          },
                          fail: function (t) {
                            console.error('canvasToTempFilePath complete:', t);
                          },
                        },
                        a,
                      );
                    }, 100)
                  : setTimeout(function () {
                      d.requestAnimationFrame(e);
                    }, 100));
            }),
              r.getImgs(d, s, a);
          });
      });
    },
    render: function (t, e) {
      var a = [];
      return (
        a.push(this.drawBG(t, e)),
        a.push(this.drawTop(t, e)),
        a.push(this.drawQR(t, e)),
        a.push(this.drawLogo(t, e)),
        a.push(this.drawText(t, e)),
        this.updataToast(a),
        !!a.every(function (t) {
          return t;
        })
      );
    },
    drawBG: function (t, e) {
      var a = this.data.P_rpx;
      return (
        this.drawFillRect(e, 0, 0, t.width, t.height, 0, 0, '#FFF'),
        this.drawFillRect_radius(
          e,
          303 * a,
          108 * a,
          144 * a,
          144 * a,
          12 * a,
          '#DDDDDD',
          !0,
        ),
        e.restore(),
        !0
      );
    },
    drawLogo: function (t, e) {
      var a = this.data,
        r = a.P_imgs,
        i = a.P_rpx;
      if (!r.wx_logo) return !1;
      var o = r.wx_logo;
      return (
        e.drawImage(
          o,
          0,
          0,
          o.width,
          o.height,
          314 * i,
          613 * i,
          122 * i,
          122 * i,
        ),
        e.restore(),
        !0
      );
    },
    drawQR: function (t, e) {
      var a = this.data,
        r = a.P_imgs,
        i = a.P_rpx;
      if (!r.qr) return !1;
      var o = r.qr;
      return (
        e.drawImage(
          o,
          0,
          0,
          o.width,
          o.height,
          104 * i,
          402 * i,
          542 * i,
          542 * i,
        ),
        e.restore(),
        !0
      );
    },
    drawTop: function (t, e) {
      var a = this.data,
        r = a.P_imgs,
        i = a.P_rpx;
      if (!r.head_img) return !1;
      var o = r.head_img;
      e.drawImage(
        o,
        0,
        0,
        o.width,
        o.height,
        309 * i,
        114 * i,
        134 * i,
        134 * i,
      );
      var n = this.data.share_data.chatroom_name;
      n.length > 12 && (n = n.slice(0, 12) + '...');
      var s = '群聊: '.concat(n);
      (e.font = ''.concat(40 * i, 'px PingFangSC-Regular, PingFang SC')),
        (e.fillStyle = 'rgba(32, 33, 36, 1)');
      var c = parseInt(e.measureText(s).width),
        h = (t.width - c) / 2;
      return e.fillText(s, h, 324 * i), e.restore(), !0;
    },
    drawText: function (t, e) {
      var a = this.data,
        r = a.P_rpx,
        i = a.share_data.qrcode_expired_at,
        o = '该二维码在'.concat(i, '前有效，');
      (e.font = ''.concat(28 * r, 'px PingFangSC-Regular, PingFang SC')),
        (e.fillStyle = 'rgba(153, 153, 153, 0.7)');
      var n = parseInt(e.measureText(o).width),
        s = parseInt(e.measureText('重新进入将更新').width),
        c = t.width,
        h = (c - n) / 2,
        d = (c - s) / 2;
      return (
        e.fillText(o, h, 1014 * r),
        e.fillText('重新进入将更新', d, 1054 * r),
        e.restore(),
        !0
      );
    },
  },
});
